/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.system.impl;

import dao.system.SystemGroupDao;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import model.system.SystemGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.system.SystemGroupService;
import utility.NameValueList;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@Service("systemGroupService")
@Transactional(readOnly=true)
public class SystemGroupServiceImpl implements SystemGroupService{
    @Autowired SystemGroupDao systemGroupDao;
    @Transactional(readOnly=false)
    @Override
    public boolean save(SystemGroup systemGroup) {
        return systemGroupDao.save(systemGroup);
    }

    @Override
    public boolean delete(SystemGroup systemGroup) {
        return systemGroupDao.delete(systemGroup);
    }

    @Override
    public SystemGroup getSystemGroup(int id) {
        return systemGroupDao.getById(id);
    }

    @Override
    public SystemGroup getSystemGroup(String kode) {
        return systemGroupDao.getByKode("group_code", kode);
    }

    @Override
    public List<SystemGroup> getSystemGroup() {
        return systemGroupDao.getAll();
    }

    @Override
    public List<SystemGroup> getSystemGroup(int start, int num) {
        return systemGroupDao.getAll(start, num);
    }

    @Override
    public List<SystemGroup> getAll(int start, int num, String params, String orderBy) {
        return systemGroupDao.getAll(start, num, params, orderBy);
    }

    @Override
    public DefaultComboBoxModel<NameValueList> getCombo(String params, String orderBy, boolean withAll) {
        DefaultComboBoxModel rs=new DefaultComboBoxModel();
        List<SystemGroup> groups = this.getAll(0, 1000, params, orderBy);
        if (withAll)
            rs.addElement(new NameValueList("-1", "Semua"));
        for (SystemGroup group : groups){
            //
            rs.addElement(new NameValueList(String.valueOf(group.getGroupId()), group.getGroupName()));
        }
        return rs; 
    }

    @Override
    public int getSelectedIndex(DefaultComboBoxModel<NameValueList> model, String key) {
        int rs = -1;
        
        for (int i =0; i< model.getSize();i++){
            NameValueList nm = (NameValueList)model.getElementAt(i);
            if (nm.getKey().equals(key)){
                rs = i;
                break;
            }
        }
        return rs;
    }
    
}
